package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	MyCopy("cp6_file/write.txt", "cp6_file/write_copy2.txt")
}

func MyCopy(srcPath string, dstPath string) {
	srcFp, err := os.Open(srcPath)
	if err != nil {
		fmt.Println(err)
	}
	defer srcFp.Close()

	dstFp, err := os.Create(dstPath)
	if err != nil {
		fmt.Println(err)
	}
	defer dstFp.Close()

	//使用缓冲输入输出流
	reader := bufio.NewReader(srcFp)
	writer := bufio.NewWriter(dstFp)
	buf := make([]byte, 1024)
	for {
		len, err := reader.Read(buf)
		writer.Write(buf[:len])
		writer.Flush()
		if err == io.EOF { //读到文件结尾
			break
		} else if err != nil {
			fmt.Println("复制文件发生错误：", err)
		}
	}
}
